// Copyright 2013 Stephane SOPPERA
//
// This file is part of 3ds-image-uploader.
//
// 3ds-image-uploader is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// 3ds-image-uploader is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with 3ds-image-uploader.  If not, see <http://www.gnu.org/licenses/>.

function setUploadPanelVisibility(visibility, focusInput) {
    var up = document.getElementById("uploadPanel");

    // first check current visibility state of the panel
    var orgVisibility = up.style.display !== "none";
    if (orgVisibility === visibility)
	return;

    var animationStep = 17;
    var animationLength = 150;
    var maxCallcount = animationLength / animationStep + 5;

    var animationStart = new Date();
    var callCount = 0;

    // we can't get clientHeight when not yet visible so we show
    // it before setting its top value
    up.style.display = "block";

    // get the height of the upload panel
    var h = up.clientHeight;

    // start an interval to do the animation
    var itv = setInterval(function () {
	++callCount;
	var elapsed = new Date() - animationStart;

	// we stop the animation as soon as either we have been called
	// too many times or when we have reached the end of animation
	// time
	if (elapsed >= animationLength || callCount > maxCallcount) {
	    // ensure we are no more called
	    clearInterval(itv);
	    
	    // in all cases reset top value to 0
	    up.style.top = "0";

	    // if we were hiding the panel, hide it now
	    if (! visibility)
		up.style.display = "none";

	    // update style of toggle button
	    var uploadButton = document.getElementById("uploadButton");
	    uploadButton.className = visibility ? "pressed" : "";

	    // if requested and if we made the panel visible, give the
	    // focus to file chooser
	    if (visibility && focusInput)
		document.getElementById("filechooser").focus();

	    // ensure links to files are focusable or not focusable
	    // depending on the visibility of the panel
	    var links = document.getElementById("pageContent")
		.getElementsByTagName("a");
	    var newIndex = visibility ? -1 : 0;
	    for (var linkIdx = 0; linkIdx < links.length; ++linkIdx) {
		var link = links[linkIdx];
		link.tabIndex = newIndex;
	    }
	}

	// compute the top value
	var frac = elapsed / animationLength;
	if (frac < 0) frac = 0;
	else if (frac > 1) frac = 1;
	var top = -1 * Math.round(h * (visibility ? (1 - frac) : frac));
	up.style.top = top + "px";
    }, 17);
}

function toggleUploadPanelVisibility() {
    var up = document.getElementById("uploadPanel");

    // first check current visibility state of the panel
    var orgVisibility = up.style.display !== "none";

    // chane panel visibility
    setUploadPanelVisibility(! orgVisibility, true);
}

function updateDates() {
    var spans = document.getElementsByTagName("span");
    for (var spanIdx = 0; spanIdx < spans.length; ++spanIdx) {
	var span = spans[spanIdx];
	if (span.className === "date") {
	    var date = new Date(span.innerHTML);
	    span.innerHTML = date.toLocaleString();
	}
    }
}
